package fr.pud.client.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import fr.pud.client.controller.routes.ChooseRouteColor;
import fr.pud.client.controller.routes.DisplayOrHideAMapRoute;
import fr.pud.client.i18n.LanguagesConstants;

/**
 * Apr 14, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         FrameToHideOrDisplayRoute.java
 *         This class creates a frame wich allows to the suer to select the
 *         route he wants to hide/display.
 */
public class FrameToHideOrDisplayRoute extends JFrame {
    /**
     * 
     */
    private static final long    serialVersionUID        = -4153733977283678461L;
    /**
     * An arrayList of checkBox to know the route selected by the user
     */
    private ArrayList<JCheckBox> checkBoxUserChoise      = new ArrayList<JCheckBox>();
    /**
     * An ArrayList of JButton to choose the color of the route
     */
    private ArrayList<JButton>   listButtonToChooseColor = new ArrayList<JButton>();

    /**
     * A Frame to choose the route to hide or display
     */
    public FrameToHideOrDisplayRoute() {
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.prepareFrame();
        this.setVisible(true);
        this.pack();
        this.placeFrame();
    }

    private void placeFrame() {
        this.setSize(new Dimension(500, (int) (Toolkit.getDefaultToolkit()
                .getScreenSize().getHeight() / 4)));
        this.setLocation((int) Toolkit.getDefaultToolkit().getScreenSize()
                .getWidth()
                - this.getWidth(), JMapViewerExtended.getJMapViewer()
                .getLocation().y);
    }

    private void prepareFrame() {
        JPanel globalPanel = new JPanel();
        // ** The button to select all the checkBox
        JButton selectAll = new JButton();
        selectAll.setAction(new SelectAll(this.checkBoxUserChoise));
        globalPanel.setLayout(new BoxLayout(globalPanel, BoxLayout.Y_AXIS));
        // The description of the use of the frame
        JLabel description = new JLabel(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.SELECT_ROUTE_CHECKBOX));
        JPanel header = new JPanel();
        header.setLayout(new BoxLayout(header, BoxLayout.Y_AXIS));
        header.add(description);
        header.add(selectAll);
        globalPanel.add(header);
        // Add the checkBox to the panel with a label corresponding to the route
// description.
        int index = 0;
        for (MapRoute mapRoute : MapRoutes.getInstance().getRoutes()) {
            JPanel panel = new JPanel(new FlowLayout());
            JPanel colorAndCheckBox = new JPanel();
            JPanel checkAndLabel = new JPanel();
            colorAndCheckBox.setLayout(new BoxLayout(colorAndCheckBox,
                    BoxLayout.Y_AXIS));
            JLabel hide = new JLabel(
                    LanguagesConstants.LANGUAGE
                            .getString(LanguagesConstants.HIDE_THE_ROUTES));
            JTextArea textArea = new JTextArea(mapRoute.toString());
            textArea.setEditable(false);
            JCheckBox checkBox = new JCheckBox("" + index,
                    !mapRoute.isVisible());
            checkBox.addChangeListener(new DisplayOrHideAMapRoute());
            this.checkBoxUserChoise.add(checkBox);
            colorAndCheckBox.add(new JButton(new ChooseRouteColor(mapRoute)));
            checkAndLabel.add(hide);
            checkAndLabel.add(checkBox);
            colorAndCheckBox.add(checkAndLabel);
            panel.add(colorAndCheckBox);
            JScrollPane scroll = new JScrollPane(textArea);
            scroll.setBorder(BorderFactory.createLineBorder(
                    mapRoute.getColor(), 2));
            panel.add(scroll);
            globalPanel.add(panel);
            index++;
        }
        this.getContentPane().add(new JScrollPane(globalPanel),
                BorderLayout.CENTER);
    }

    /**
     * @return the listButtonToChooseColor
     */
    public ArrayList<JButton> getListButtonToChooseColor() {
        return listButtonToChooseColor;
    }

    /**
     * @param listButtonToChooseColor
     *            the listButtonToChooseColor to set
     */
    public void setListButtonToChooseColor(
            ArrayList<JButton> listButtonToChooseColor) {
        this.listButtonToChooseColor = listButtonToChooseColor;
    }
}

/**
 * Apr 15, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         FrameToHideOrDisplayRoute.java
 *         This class enables to select all the checkBoxes of a arrayList of
 *         checkBoxes.
 */
@SuppressWarnings("serial")
class SelectAll extends AbstractAction {
    private ArrayList<JCheckBox> listCheckBox         = new ArrayList<JCheckBox>();
    private boolean              isAlreadyAllSelected = false;

    /**
 * 
 */
    public SelectAll(ArrayList<JCheckBox> aCheckBoxList) {
        this.listCheckBox = aCheckBoxList;
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.SELECT_ALL));
    }

    @Override
    public void actionPerformed(ActionEvent aArg0) {
        if (!this.isAlreadyAllSelected) {
            for (JCheckBox box : this.listCheckBox) {
                box.setSelected(true);
            }
            this.isAlreadyAllSelected = true;
        }
        else {
            for (JCheckBox box : this.listCheckBox) {
                box.setSelected(false);
            }
            this.isAlreadyAllSelected = false;
        }
    }
}
